﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using SilverlightWcfDataServiceProject.ServiceReference1;
using System.Data.Services.Client;

namespace SilverlightWcfDataServiceProject
{
    public partial class ViewOrders : Page
    {
        InvEntities1 Inv;
        DataServiceCollection<Order> Orders;
        public ViewOrders()
        {
            InitializeComponent();
            Inv = new InvEntities1(new Uri("http://localhost:51525/WcfDataService1.svc"));
            var orderq = from i in Inv.Orders
                        select i;
            Orders = new DataServiceCollection<Order>();
            Orders.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(Orders_LoadCompleted);
            Orders.LoadAsync(orderq);

        }

          void Orders_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            ordersListBox.ItemsSource = Orders;
            ordersListBox.DisplayMemberPath = "ID";
            //gridItem.DataContext = Items.First<Item>();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

            // Do not load your data at design time.
            // if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            // {
            // 	//Load your data here and assign the result to the CollectionViewSource.
            // 	System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
            // 	myCollectionViewSource.Source = your data
            // }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ((Order)ordersListBox.SelectedItem).Status = true;
            Inv.BeginSaveChanges(i => MessageBox.Show("changed"), ((Order)ordersListBox.SelectedItem));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ((Order)ordersListBox.SelectedItem).Status = false;
            Inv.BeginSaveChanges(i => MessageBox.Show("changed"), ((Order)ordersListBox.SelectedItem));
        }

        private void ordersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            grid1.DataContext = (Order)ordersListBox.SelectedItem;
        }


    }
}
